package nextel.agps;
 

/**
 *
 * @author eespinoza
 */
public class ControlAgps {

    public static ControlAgps agpsContainer = new ControlAgps();
    public final int OPC_LED_ROJO = 0xff0000;
    public final int OPC_LED_AMARILLO = 0xffff00;
    public final int OPC_LED_VERDE = 0x00d900;
    public AGPSBean currentGPS;
    private boolean bandera = false;
    private Thread threadLeds;
    private AgpsMvcListener mvcComponent;

    private ControlAgps() {
    }

    public static ControlAgps getInstance() {
        return agpsContainer;
    }

    /********************************************************************************************
     * aGPS
     ********************************************************************************************/
    public void initializeAGPS(String tiempoEnvio) {

        currentGPS = new AGPSBean();
        //currentGPS.setMposition(AGPSBean.DELAY_LOW);
        GPSThread gpsThread = new GPSThread(currentGPS, false, Integer.parseInt(tiempoEnvio) * 1000);
        gpsThread.start();

    }

    /* */
    private synchronized void evaluarQoSGPS() {
      //  System.out.println("sigue vivo");
        if (mvcComponent != null && currentGPS != null) {
            mvcComponent.getIndicadorAGPS().setColorSenal(verificaSenal());
            mvcComponent.getIndicadorAGPS().setColorTiempo(verificaTiempo());
        }
    }

    public void finalizarLeds() {
        bandera = false;
    }

    public void iniciarLeds(AgpsMvcListener agpsMvcListener) {

        mvcComponent = agpsMvcListener;

        updateLets();


    }

    private void joinThred() throws Exception {
        if (threadLeds != null && threadLeds.isAlive()) {
            threadLeds.join();
            threadLeds = null;
        }
    }

    private void updateLets() {
        try {
            bandera = false;
            joinThred();
            bandera = true;
            threadLeds = new Thread() {

                public void run() {
                    try {
                        while (bandera) {
                            evaluarQoSGPS();
                            sleep(1500);
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            };
            
            threadLeds.start( );

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //EXACTITUD DEL FIX: RANGO DE ERROR DEL FIX OBTENIDO
    private int verificaSenal() {
        int senal = currentGPS.exactitudLatLon / 1000;

      // System.out.println(currentGPS.asistUsada + "|" + senal + " '"+currentGPS.latitude +" '" +currentGPS.longitude + "|" + currentGPS.numSatelites+"|" + currentGPS.velocidad);

       // multiplicados por 4
        if (currentGPS.asistUsada && senal < 80) {
            return OPC_LED_VERDE;
        } else if (currentGPS.asistUsada && senal <= 200 && senal >= 80) {
            return OPC_LED_AMARILLO;
        } else {
            return OPC_LED_ROJO;
        }
    }

    private int verificaTiempo() {
        long tempo = (System.currentTimeMillis() - currentGPS.timeStamp) / 1000;

       // System.out.print("|tiempo=" + tempo);
        
        if (currentGPS.asistUsada && tempo < 15) {
            return OPC_LED_VERDE;
        } else if (currentGPS.asistUsada && tempo <= 25 && tempo >= 15) {
            return OPC_LED_AMARILLO;
        } else {
            return OPC_LED_ROJO;
        }
    }
    
 
    /**********************************************************************************************************
     * FIN AGPS
     **********************************************************************************************************/
}
